﻿@using Kojarskiy.Core
@using Kojarskiy.Models
@model IEnumerable<Kojarskiy.Models.SiteMap>

@{
    ViewBag.Title = "Страницы сайта";
}
@Html.ActionLink("Администрирование", "Index","Admin") | @ViewBag.Title
<div style="clear: both;"></div>

<h1 class="f_l">@ViewBag.Title</h1>

<div class="form-group f_r">
    <button class="btn btn-sm btn-success" type="button" onclick="location.href='@Url.Action("Create", "SiteMap", new { rootId = ViewBag.RootId })'">Добавить страницу</button>
</div>
<div style="clear: both;"></div>
<div class="container-fluid">

    <div class="col-md-3">      
        @Html.Partial("_SiteTree", "SiteMap")
    </div>

    <div class="col-md-9">
        @if (ViewBag.PreRootId != null)
        {
            <a href="@Url.Content("~/sitemap/index/?rootId=" + ViewBag.PreRootId)">
                <img src="@Url.Content("~/Content/Images/uplevel-icon.png")" style="vertical-align: middle; padding: 5px; width: 25px;"/>
            </a>
        }         
        Текущий путь: @Html.Raw(SiteMapHelper.GetNodePathString((int?)ViewBag.RootId))
        <table class="table table-hover">
            <tr>
                <th>
                    Название
                </th>
                <th>
                    Путь
                </th>
                <th>
                    Активна
                </th>
                <th>
                    Порядок
                </th>
                <th>
                    Действия
                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.ActionLink(item.PageName, "Index", "SiteMap", new { rootId = item.SiteMapId },"")
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Path)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Show)
                    </td>
                    <td>
                        @if(item.ParentId != null)
                        {
                            <a onclick="OrderDown(@item.SiteMapId)"><span class="glyphicon glyphicon-arrow-down" style="color:blue; cursor: pointer;" onclick=""></span></a>
                            <a onclick="OrderUp(@item.SiteMapId)"><span class="glyphicon glyphicon-arrow-up" style="color:blue; cursor: pointer;" onclick=""></span></a>
                        }
                    </td>
                    <td>
                        <button class="btn btn-xs btn-warning" type="button" onclick="location.href='@Url.Action("Edit", "SiteMap", new { id = item.SiteMapId })'">Редактировать</button>
                        @if(item.ParentId != null)
                        {
                            <button class="btn btn-xs btn-danger" type="button" onclick="location.href='@Url.Action("Delete", "SiteMap", new { id = item.SiteMapId })'">Удалить</button>
                        }
                    </td>
                </tr>
            }
        </table>
    </div>
</div>
<script type="text/javascript">
    function OrderDown(siteMapId) {
        $.ajax({
            type: "POST",
            url: "/SiteMap/OrderDown",
            data: {siteMapId: siteMapId},
            success: location.reload()
        });
    }
    function OrderUp(siteMapId) {
        $.ajax({
            type: "POST",
            url: "/SiteMap/OrderUp",
            data: { siteMapId: siteMapId },
            success: location.reload()
        });
    }
</script>